<template>
  <div class="alertBox">
    <div class="alertMask" v-show="isShowMask && show"></div>
    <transition :name="translate">
      <div class="box" :class="position" v-show="show">{{text}}</div>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'toast',
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    text: {
      type: String,
      default: 'loadding',
    },
    position: {
      type: String,
      default: 'middle',
    },
    isShowMask: {
      type: Boolean,
      default: false,
    },
    time: {
      type: Number,
      default: 1500,
    },
    transition: {
      type: Boolean,
      default: true,
    },
  },
  computed: {
    translate() {
      if (!this.transition) {
        return '';
      }
      if (this.position === 'top') {
        return 'translate-top';
      } else if (this.position === 'middle') {
        return 'translate-middle';
      } else if (this.position === 'bottom') {
        return 'translate-bottom';
      }
      return '';
    },
  },
  watch: {
    show(val) {
      if (val) {
        clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
          this.show = false;
        }, this.time);
      }
    },
  },
};
</script>

<style>
/* .alertBox {
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  z-index: 1000;
  background-color: #f0f;
} */
.box {
  position: fixed;
  top: 50%;
  left: 50%;
  width: 100px;
  height: 100px;
  margin-left: -50px;
  margin-top: -50px;
  background: rgba(0, 0, 0, 0.5);
  text-align: center;
  line-height: 100px;
  color: #fff;
  font-size: 16px;
  z-index: 5000;
}
.box.top {
  top: 50px;
  margin-top: 0;
}
.box.center {
  top: 50%;
  margin-top: -100px;
}
.box.bottom {
  top: auto;
  bottom: 50px;
  margin-top: 0;
}
/* .alert-mask {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 4999;
} */
/* .fadeIn-enter-active,
.fadeIn-leave-active {
  transition: opacity 0.3s;
}
.fadeIn-enter,
.fadeIn-leave-active {
  opacity: 0;
} */
.translate-top-enter-active,
.translate-top-leave-active {
  transition: all 0.3s cubic-bezier(0.36, 0.66, 0.04, 1);
}
.translate-top-enter,
.translate-top-leave-active {
  transform: translateY(-50%);
  opacity: 0;
}
.translate-middle-enter-active,
.translate-middle-leave-active {
  transition: all 0.3s cubic-bezier(0.36, 0.66, 0.04, 1);
}
.translate-middle-enter,
.translate-middle-leave-active {
  transform: translateY(80%);
  opacity: 0;
}
.translate-bottom-enter-active,
.translate-bottom-leave-active {
  transition: all 0.3s cubic-bezier(0.36, 0.66, 0.04, 1);
}
.translate-bottom-enter,
.translate-bottom-leave-active {
  transform: translateY(100%);
  opacity: 0;
}
</style>
